const Router = require('@koa/router');
const { checkAuth, checkPermission } = require('../middlewares/auth');
const fileController = require('../controllers/file.controller');

const router = new Router({ prefix: '/api/v1/files' });

/**
 * @swagger
 * /api/v1/files/upload:
 *   post:
 *     tags: [文件管理]
 *     summary: 上传文件
 *     description: 上传一个或多个文件到指定存储桶
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             required:
 *               - files
 *             properties:
 *               files:
 *                 type: array
 *                 items:
 *                   type: string
 *                   format: binary
 *                 description: 要上传的文件
 *               bucket:
 *                 type: string
 *                 description: 目标存储桶名称
 *                 default: "default"
 *               path:
 *                 type: string
 *                 description: 存储桶中的目标路径
 *                 default: ""
 *               access:
 *                 type: string
 *                 enum: [private, public-read]
 *                 description: 文件访问级别
 *                 default: "private"
 *     responses:
 *       201:
 *         description: 文件上传成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     files:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           id:
 *                             type: string
 *                             example: "file_001"
 *                           url:
 *                             type: string
 *                             example: "https://storage.example.com/files/image.jpg"
 *                           name:
 *                             type: string
 *                             example: "image.jpg"
 *                           size:
 *                             type: integer
 *                             example: 1024
 *                           type:
 *                             type: string
 *                             example: "image/jpeg"
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       500:
 *         description: 上传失败
 */
router.post('/upload', checkAuth, checkPermission('file:write'), fileController.upload);

/**
 * @swagger
 * /api/v1/files/{fileId}:
 *   get:
 *     tags: [文件管理]
 *     summary: 下载文件
 *     description: 下载或查看指定的文件
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: fileId
 *         required: true
 *         schema:
 *           type: string
 *         description: 文件ID
 *       - in: query
 *         name: download
 *         schema:
 *           type: boolean
 *         description: 是否作为附件下载（true）或直接查看（false）
 *     responses:
 *       200:
 *         description: 文件内容
 *         content:
 *           application/octet-stream:
 *             schema:
 *               type: string
 *               format: binary
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 文件不存在
 */
router.get('/:fileId', checkAuth, checkPermission('file:read'), fileController.download);

/**
 * @swagger
 * /api/v1/files/{fileId}:
 *   delete:
 *     tags: [文件管理]
 *     summary: 删除文件
 *     description: 删除指定的文件
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: fileId
 *         required: true
 *         schema:
 *           type: string
 *         description: 文件ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 message:
 *                   type: string
 *                   example: "文件删除成功"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 文件不存在
 */
router.delete('/:fileId', checkAuth, checkPermission('file:delete'), fileController.delete);

/**
 * @swagger
 * /api/v1/files/{fileId}/info:
 *   get:
 *     tags: [文件管理]
 *     summary: 获取文件信息
 *     description: 获取指定文件的详细信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: fileId
 *         required: true
 *         schema:
 *           type: string
 *         description: 文件ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: string
 *                       example: "file_001"
 *                     name:
 *                       type: string
 *                       example: "image.jpg"
 *                     size:
 *                       type: integer
 *                       example: 1024
 *                     type:
 *                       type: string
 *                       example: "image/jpeg"
 *                     url:
 *                       type: string
 *                       example: "https://storage.example.com/files/image.jpg"
 *                     createdAt:
 *                       type: string
 *                       format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 文件不存在
 */
router.get('/:fileId/info', checkAuth, checkPermission('file:read'), fileController.getInfo);

/**
 * @swagger
 * /api/v1/files:
 *   get:
 *     tags: [文件管理]
 *     summary: 获取文件列表
 *     description: 获取指定存储桶和路径下的文件列表
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: bucket
 *         schema:
 *           type: string
 *         description: 存储桶名称
 *         default: "default"
 *       - in: query
 *         name: path
 *         schema:
 *           type: string
 *         description: 文件路径
 *         default: ""
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *         description: 页码
 *         default: 1
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *         description: 每页数量
 *         default: 10
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     files:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           id:
 *                             type: string
 *                           name:
 *                             type: string
 *                           size:
 *                             type: integer
 *                           type:
 *                             type: string
 *                           url:
 *                             type: string
 *                           createdAt:
 *                             type: string
 *                             format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/', checkAuth, checkPermission('file:read'), fileController.list);

/**
 * @swagger
 * /api/v1/files/buckets:
 *   post:
 *     tags: [文件管理]
 *     summary: 创建存储桶
 *     description: 创建新的文件存储桶
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *             properties:
 *               name:
 *                 type: string
 *                 description: 存储桶名称
 *               access:
 *                 type: string
 *                 enum: [private, public-read]
 *                 description: 访问权限
 *                 default: "private"
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     name:
 *                       type: string
 *                     access:
 *                       type: string
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.post('/buckets', checkAuth, checkPermission('file:write'), fileController.createBucket);

/**
 * @swagger
 * /api/v1/files/buckets/{name}:
 *   delete:
 *     tags: [文件管理]
 *     summary: 删除存储桶
 *     description: 删除指定的文件存储桶
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: name
 *         required: true
 *         schema:
 *           type: string
 *         description: 存储桶名称
 *       - in: query
 *         name: force
 *         schema:
 *           type: boolean
 *         description: 是否强制删除非空存储桶
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 message:
 *                   type: string
 *                   example: "存储桶删除成功"
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 存储桶不存在
 */
router.delete('/buckets/:name', checkAuth, checkPermission('file:delete'), fileController.deleteBucket);

/**
 * @swagger
 * /api/v1/files/buckets/{name}:
 *   get:
 *     tags: [文件管理]
 *     summary: 获取存储桶信息
 *     description: 获取指定存储桶的详细信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: name
 *         required: true
 *         schema:
 *           type: string
 *         description: 存储桶名称
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     name:
 *                       type: string
 *                     access:
 *                       type: string
 *                     size:
 *                       type: integer
 *                     fileCount:
 *                       type: integer
 *                     createdAt:
 *                       type: string
 *                       format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 存储桶不存在
 */
router.get('/buckets/:name', checkAuth, checkPermission('file:read'), fileController.getBucketInfo);

/**
 * @swagger
 * /api/v1/files/buckets/{name}/access:
 *   put:
 *     tags: [文件管理]
 *     summary: 更新存储桶访问权限
 *     description: 更新指定存储桶的访问权限设置
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: name
 *         required: true
 *         schema:
 *           type: string
 *         description: 存储桶名称
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - access
 *             properties:
 *               access:
 *                 type: string
 *                 enum: [private, public-read, public-write]
 *                 description: 新的访问权限
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     name:
 *                       type: string
 *                     access:
 *                       type: string
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 存储桶不存在
 */
router.put('/buckets/:name/access', checkAuth, checkPermission('file:write'), fileController.updateBucketAccess);

/**
 * @swagger
 * /api/v1/files/{fileId}/presigned-url:
 *   get:
 *     tags: [文件管理]
 *     summary: 生成预签名URL
 *     description: 生成文件的预签名下载URL
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: fileId
 *         required: true
 *         schema:
 *           type: string
 *         description: 文件ID
 *       - in: query
 *         name: expiresIn
 *         schema:
 *           type: integer
 *           default: 3600
 *         description: URL有效期（秒）
 *     responses:
 *       200:
 *         description: 生成成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 data:
 *                   type: object
 *                   properties:
 *                     url:
 *                       type: string
 *                       example: "https://storage.example.com/files/temp/image.jpg?signature=xxx"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 文件不存在
 */
router.get('/:fileId/presigned-url', checkAuth, checkPermission('file:read'), fileController.generatePresignedUrl);

module.exports = router; 